package fr.cpe.j2ee.projet.war;

import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.primefaces.model.TreeNode;

import com.sun.jdi.connect.Connector.SelectedArgument;

import fr.cpe.j2ee.projet.ejb.ActivitySession;
import fr.cpe.j2ee.projet.model.Activity;
import fr.cpe.j2ee.projet.model.Employee;
import fr.cpe.j2ee.projet.model.Project;

public class ProjectBean 
{
	@EJB
	private ActivitySession activitySession;
	
	private int proId;
	private int proBudget;
	private String proDescription;
	private String proName;
	private ActivityListBean activities;
	private Employee projectManager;
	
	private LoginBean loginBean;
	
	public ProjectBean(){
	}
	
	public ProjectBean(int proId, String proName, int proBudget, String proDescription, ActivityListBean activities)
	{
		this.proId = proId;
		this.proName = proName;
		this.proBudget = proBudget;
		this.proDescription = proDescription;
		this.activities = activities;
		this.projectManager = null;
	}
	
	public void addActivity(String tache, TreeNode proID)
	{
		System.out.println("add activity method");
//		Project pro = new Project();
		Project pro = (Project) proID.getData();
		
		activitySession.createActivity(pro, pro.getEmployee(), 0, 0, tache);
		
		try {
			ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
    		context.redirect(context.getRequestContextPath() + "/xhtml/user/userProject.xhtml");
		} catch (IOException e) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("La redirection a ��chou�� !"));	
			e.printStackTrace();
		}
	}
	
	
	@PostConstruct
	public void postContruct() {
		// Récupération de l'employé connecté pour récupération de la liste des projets qu'il manage
		this.projectManager = loginBean.getEmployee();
	}
	
	public ActivityListBean getActivities() {
		return activities;
	}
	public void setActivities(ActivityListBean activities) {
		this.activities = activities;
	}
	public int getProBudget() {
		return proBudget;
	}
	public void setProBudget(int proBudget) {
		this.proBudget = proBudget;
	}
	public String getProDescription() {
		return proDescription;
	}
	public void setProDescription(String proDescription) {
		this.proDescription = proDescription;
	}
	public String getProName() {
		return proName;
	}
	public void setProName(String proName) {
		this.proName = proName;
	}
	public Employee getProjectManager() {
		return projectManager;
	}
	public void setProjectManager(Employee projectManager) {
		this.projectManager = projectManager;
	}
	public int getProId() {
		return proId;
	}
	public void setProId(int proId) {
		this.proId = proId;
	}

	public void setLoginBean(LoginBean loginBean) {
		this.loginBean = loginBean;
	}
}
